import requests
import re

# 获取代理 IP
# 提取代理API接口，获取1个代理IP
api_url = ""


proxy_ip = requests.get(api_url).text

username = ""
password = " "
proxies = {
    "http": f"http://{username}:{password}@{proxy_ip}/",
    "https": f"http://{username}:{password}@{proxy_ip}/"
}

# 测试代理
target_url = "https://dev.kdlapi.com/testproxy"
try:
    response = requests.get(target_url, proxies=proxies)
    if response.status_code == 200:
        print(response.text)
    else:
        print(f"测试代理时请求失败，状态码: {response.status_code}")
except requests.RequestException as e:
    print(f"测试代理时出错: {e}")

# 设置请求头
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 "
                  "Safari/537.36 Edg/135.0.0.0"
}


try:
    res = requests.get("https://www.zdaye.com/free/", proxies=proxies, headers=headers)
    res.raise_for_status()

    pattern = r'<td>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})</td>'
    ips = re.findall(pattern, res.text)
    for ip in ips:
        print(ip)
except requests.RequestException as e:
    print(f"请求目标网页时出错: {e}")
except Exception as e:
    print(f"发生未知错误: {e}")
